const connectiondb = require("../sql/index.js");

//获取所有管理员信息
async function queryAdmin(req,res){
    const result = await connectiondb("select * from admin",[])
    res.send({code:200,msg:"获取管理员信息成功",data:result})
}

//删除管理员信息
async function delAdmin(req,res) {
    const {id} = req.body;
    const sql = "delete from admin where id = ?"
    await connectiondb(sql,[id])
    res.send({code:200,msg:"删除成功"})
}

//根据id获取管理员信息
async function queryAdminById(req,res){
    const {id} = req.body;
    const sql = "select * from admin where id =?";
    const result = await connectiondb(sql,[id])
    res.send({code:200,msg:"获取管理员信息成功",data:result[0]})
}

//修改管理员信息
async function updateAdmin(req,res) {
    const {id,name,password} = req.body;
    const sql = "update admin set name =?,password =? where id =?";
    await connectiondb(sql,[name,password,id])
    res.send({code:200,msg:"修改管理员信息成功"})
}

//添加管理员信息
async function addAdmin(req, res) {
    const {name,password} = req.body;
    const sql = "insert into admin values(null,?,?,null)";
    await connectiondb(sql,[name,password])
    res.send({code:200,msg:"添加管理员信息成功"})
}

//模糊查询管理员信息

async function likename(req,res){
    const {name} = req.body;
    const sql = `select * from admin where name like '%${name}%'`;
    const result = await connectiondb(sql,[])
    res.send({code:200,msg:"模糊查询管理员信息成功",data:result})
}


module.exports = {
    delAdmin,
    queryAdmin,
    queryAdminById,
    updateAdmin,
    addAdmin,
    likename
}